package com.ruoyi.web.util;

import com.alibaba.druid.filter.stat.StatFilter;
import com.alibaba.druid.wall.WallFilter;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.template.source.ClassPathSourceFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.awt.*;
import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class FileUtil {
    private final Logger logger = LoggerFactory.getLogger(getClass());
    public static List<String> getFilename(String path, Record info) {
        File file = new File(path);
        File[] tempList = file.listFiles();
        List<String> filenames = new ArrayList<String>();
        if(tempList==null) {
            return filenames;
        }
        for (File temp : tempList) {
            if(temp.getName().toUpperCase().contains(info.getStr("pe_id"))) {
                filenames.add(temp.getPath());
            }
        }
        return filenames;
    }

    /**
     * 处理检验接口 PE_PERSONAL_ITEM_DICT 表 PE_DATE 数据不同步问题
     * @param args
     * @throws AWTException
     */
    public static void main(String[] args) throws AWTException {
        System.out.println("启动主函数");

//        //启动数据库连接
        String username="system";
        String password="peis";
        String url="jdbc:oracle:thin:@192.168.22.222:1521:peis";
        DruidPlugin druidPlugin = new DruidPlugin(url, username, password);
        // 加强数据库安全
        WallFilter wallFilter = new WallFilter();
        wallFilter.setDbType("oracle");
        druidPlugin.addFilter(wallFilter);
        // 添加 StatFilter 才会有统计数据
        druidPlugin.addFilter(new StatFilter());
        ActiveRecordPlugin arp = new ActiveRecordPlugin(druidPlugin);
        arp.setShowSql(false);
        arp.getEngine().setSourceFactory(new ClassPathSourceFactory());
        druidPlugin.start();
        arp.start();
//        List<Record> labResultList= Db.find("select t.test_no,t.result_date_time from lab_result t where t.result_date_time > to_date('2021-04-01', 'yyyy-mm-dd') group by t.test_no,t.result_date_time");
        List<Record> labResultList= Db.find("select * from (select x.*,rownum rn from (select t.test_no,t.result_date_time from lab_result t where t.result_date_time > to_date('2021-04-01', 'yyyy-mm-dd')  group by t.test_no,t.result_date_time ) x) where rn > 7928");

        List<Record> PERSONALList=new ArrayList<>();
        System.out.println("开始数据循环，准备数据处理");
        String inSql="";
        for(Record data:labResultList)
        {
            Date peDate=data.getDate("RESULT_DATE_TIME");
            String apply_no=data.getStr("TEST_NO");
            int rn=data.getInt("RN");
            Record PERSONAL_ITEM_DICT=Db.findFirst("select * from PE_PERSONAL_ITEM_DICT where apply_no=?",apply_no);
            if(PERSONAL_ITEM_DICT!=null)
            {
                if(PERSONAL_ITEM_DICT.get("PE_DATE")==null || "".equals(PERSONAL_ITEM_DICT.get("PE_DATE")))
                {
                    String updateSql="update PE_PERSONAL_ITEM_DICT set PE_DATE=? where apply_no=? ";
                    Db.update(updateSql,peDate,apply_no);
                    System.out.println("已经处理到《"+rn+"》条数据！");
                    inSql+=apply_no+"','";
                }
            }
            //PE_ID, PE_VISIT_ID, ITEM_ASSEM_CODE
        }

        System.out.println("完成数据循环 开始批处理数据");
        //Db.batchUpdate("PE_PERSONAL_ITEM_DICT","PE_ID, PE_VISIT_ID, ITEM_ASSEM_CODE",PERSONALList,100);

    }

}
